<?php
/**
 * Created by PhpStorm.
 * User: forvoid
 * Date: 5/3/2017
 * Time: 9:14 AM
 */
//$appid = "wx5d7e875d2aa6109e";
//$secret = "c7b7b37c33f691809ecd62149ab542ae";
//$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
//$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//$output = curl_exec($ch);
//
//curl_close($ch);
//
//$jsoninfo = json_decode($output, true);
//$access_token = $jsoninfo['access_token'];
//var_dump($jsoninfo);
//var_dump($access_token);
    $jsonmenu = '{
 "button":[
 {
       "name":"公共查询",
       "sub_button":[
        {
           "type":"click",
           "name":"天气查询",
           "key":"tianQi"
        },
        {
           "type":"click",
           "name":"公交查询",
           "key":"gongJiao"
        },
        {
           "type":"click",
           "name":"翻译",
           "key":"fanYi"
        }]
  },
  {
       "name":"重庆本地",
       "sub_button":[
        {
           "type":"click",
           "name":"爱上重庆",
           "key":"loveSuzhou"
        },
        {
           "type":"click",
           "name":"重庆景点",
           "key":"suzhouScenic"
        },
        {
           "type":"click",
           "name":"重庆美食",
           "key":"suzhouFood"
        },
        {
           "type":"click",
           "name":"住在重庆",
           "key":"liveSuzhou"
        }]
   },
   {
       "type":"click",
       "name":"关于我",
       "key":"aboutme"
   }]
}
        ';
    $menu = new createMenu();
    $access_token = $menu->getAccessToken();
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";

    $result = $menu->https_request($url,$jsonmenu);
//    var_dump($url);
    var_dump($result);


//}
class createMenu
{
    function https_request($url, $data)
    {
//        $curl = curl_init();
//        curl_setopt($curl, CURLOPT_URL, $url);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//        if (!empty($date)) {
//            curl_setopt($curl, CURLOPT_PORT, 1);
//            curl_setopt($curl, CURLOPT_POSTFIELDS, $date);
//        }
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        $output = curl_exec($curl);
//        curl_close($curl);
//        return $output;
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $info = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Errno'.curl_error($ch);
        }

        curl_close($ch);

        return $info;
    }

    public function getAccessToken()
    {
        $appid = "wx415a63ea29f2cf6e";
        $secret = "39e81f5756e13f215adc5f7e1566d4bc";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);

        curl_close($ch);

        $jsoninfo = json_decode($output, true);
        return $access_token = $jsoninfo['access_token'];

    }
}
?>